package com.jingdianjichi.interview.server.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jingdianjichi.interview.api.enums.EngineEnum;
import com.jingdianjichi.interview.api.req.InterviewSubmitReq;
import com.jingdianjichi.interview.api.req.StartReq;
import com.jingdianjichi.interview.api.vo.InterviewQuestionVO;
import com.jingdianjichi.interview.api.vo.InterviewResultVO;
import com.jingdianjichi.interview.api.vo.InterviewVO;
import com.jingdianjichi.interview.server.entity.po.SubjectCategory;
import com.jingdianjichi.interview.server.entity.po.SubjectInfo;
import com.jingdianjichi.interview.server.entity.po.SubjectLabel;
import com.jingdianjichi.interview.server.mapper.SubjectDao;
import com.jingdianjichi.interview.server.service.InterviewEngine;
import com.jingdianjichi.interview.server.utils.EvaluateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * ClassName:JiChiInterviewEngine
 * Package:com.jingdianjichi.interview.server.service.Impl
 * Description:
 *
 * @Author fst
 * @Create 2025/2/25 9:14
 * @Version 1.0
 */

@Service
public class JiChiInterviewEngine implements InterviewEngine
{

    private  List<SubjectLabel> labels =new ArrayList<>();

    private Map<Long, SubjectCategory> categoryMap =new HashMap<>();

    @Resource
    private SubjectDao subjectDao;


    @PostConstruct
    public void init()
    {
        labels = subjectDao.listAllLabel();

        categoryMap = subjectDao.listAllCategory().stream().collect(Collectors.toMap(SubjectCategory::getId, Function.identity()));
    }
    @Override
    public EngineEnum engineType() {
        return EngineEnum.JI_CHI;
    }

    @Override
    public InterviewVO analyse(List<String> KeyWords) {
        if(KeyWords.isEmpty())
        {
            return new InterviewVO();
        }

        List<InterviewVO.Interview> interviewList = this.labels.stream().filter
                (item -> KeyWords.contains(item.getLabelName())).map(item -> {
                    InterviewVO.Interview interview = new InterviewVO.Interview();
                    SubjectCategory category = categoryMap.get(item.getCategoryId());

                    if(category != null)
                    {
                        interview.setKeyWord(String.format("%s-%s", category.getCategoryName(), item.getLabelName()));
                    } else {
                        interview.setKeyWord(item.getLabelName());
                    }
                    interview.setCategoryId(Long.valueOf(item.getCategoryId()));
                    interview.setLabelId(item.getId());
                    return interview;
        }).collect(Collectors.toList());

        InterviewVO interviewVO = new InterviewVO();
        interviewVO.setQuestionList(interviewList);
        return interviewVO;
    }

    @Override
    public InterviewResultVO submit(InterviewSubmitReq req) {
        List<InterviewSubmitReq.Submit> submits = req.getQuestionList();
        double total = submits.stream().mapToDouble(InterviewSubmitReq.Submit::getUserScore).sum();
        double avg = total / submits.size();
        String avtTips = EvaluateUtils.avgEvaluate(avg);
        String tips = submits.stream().map(item -> {
            String keyWord = item.getLabelName();
            String evaluate = EvaluateUtils.evaluate(item.getUserScore());
            return String.format(evaluate, keyWord);
        }).distinct().collect(Collectors.joining(";"));
        InterviewResultVO vo = new InterviewResultVO();
        vo.setAvgScore(avg);
        vo.setTips(tips);
        vo.setAvgTips(avtTips);
        return vo;

    }

    @Override
    public InterviewQuestionVO start(StartReq req) {
        List<Long> ids = req.getQuestionList().stream().map(StartReq.Key::getLabelId).distinct().collect(Collectors.toList());

        if (CollectionUtils.isEmpty(ids)) {
            return new InterviewQuestionVO();
        }

        List<SubjectInfo> subjectInfoList = subjectDao.listSubjectInfoByLabelIds(ids);

        List<InterviewQuestionVO.Interview> interviewList = subjectInfoList.stream().map(item -> {
             InterviewQuestionVO.Interview interview = new InterviewQuestionVO.Interview();
             interview.setLabelName(item.getLabelName());
             interview.setSubjectAnswer(item.getSubjectAnswer());
             interview.setSubjectName(item.getSubjectName());
             interview.setKeyWord(String.format("%s-%s", item.getCategoryName(), item.getLabelName()));
             return interview;
        }).collect(Collectors.toList());

        //挑选8道题目
        if(interviewList.size()>8)
        {
            Collections.shuffle(interviewList);
            interviewList = interviewList.subList(0,8);
        }

        InterviewQuestionVO vo = new InterviewQuestionVO();
        vo.setQuestionList(interviewList);
        return vo;
    }
}

